/*
 * DesktopStrips.java
 *
 * Created on Jun 1, 2011, 1:45:10 AM
 */
package desktopstrips;

import java.awt.Dimension;
import java.awt.Toolkit;
import strips.*;

import java.io.*;
import java.net.URISyntaxException;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author mire
 */
public class DesktopStrips extends javax.swing.JFrame {

    /** Creates new form DesktopStrips */
    public DesktopStrips() {
        initComponents();

        //Center frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension size = this.getSize();
        screenSize.height = screenSize.height / 2;
        screenSize.width = screenSize.width / 2;
        size.height = size.height / 2;
        size.width = size.width / 2;
        int y = screenSize.height - size.height;
        int x = screenSize.width - size.width;
        this.setLocation(x, y);
    }

    private void stateToTextArea(JTextArea txt, ArrayDeque<Predicate> state) {
        String str = "";
        txt.removeAll();
        Iterator<Predicate> it = state.iterator();
        while (it.hasNext()) {
            Predicate element = it.next();
            str += element + "\n";
        }
        txt.append(str);
    }

    class OnlyTxtFilter extends javax.swing.filechooser.FileFilter {

        @Override
        public boolean accept(File file) {
            // Allow only directories, or files with ".txt" extension
            return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
        }

        @Override
        public String getDescription() {
            // This description will be displayed in the dialog,
            // hard-coded = ugly, should be done via I18N
            return "Текстуални документи (*.txt)";
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        authorDialog = new javax.swing.JDialog();
        authorAvatarLabel = new javax.swing.JLabel();
        authorNameLabel = new javax.swing.JLabel();
        authorIndexLabel = new javax.swing.JLabel();
        authorMailLabel = new javax.swing.JLabel();
        aboutDialog = new javax.swing.JDialog();
        aboutNameLabel = new javax.swing.JLabel();
        aboutBody = new javax.swing.JLabel();
        aboutOKButton = new javax.swing.JButton();
        aboutURL = new javax.swing.JLabel();
        helpDialog = new javax.swing.JDialog();
        helpBodyDialog = new javax.swing.JLabel();
        helpNameLabel = new javax.swing.JLabel();
        helpOKButton = new javax.swing.JButton();
        mainPanel = new javax.swing.JPanel();
        actionPreformed = new javax.swing.JLabel();
        actionPreformed.setVisible(false);
        currentStateLabel = new javax.swing.JLabel();
        currentStateLabel.setVisible(false);
        finalStateLabel = new javax.swing.JLabel();
        finalStateLabel.setVisible(false);
        jScrollPaneLeft = new javax.swing.JScrollPane();
        currentStateTextArea = new javax.swing.JTextArea();
        jScrollPaneRight = new javax.swing.JScrollPane();
        finalStateTextArea = new javax.swing.JTextArea();
        blockWorld = new BlockWorld();
        jMenuBar = new javax.swing.JMenuBar();
        FileMenu = new javax.swing.JMenu();
        Open = new javax.swing.JMenuItem();
        Exit = new javax.swing.JMenuItem();
        HelpMenu = new javax.swing.JMenu();
        Help = new javax.swing.JMenuItem();
        jSeparator = new javax.swing.JPopupMenu.Separator();
        About = new javax.swing.JMenuItem();
        Author = new javax.swing.JMenuItem();

        fileChooser.setDialogTitle("Изаберите пример");
        fileChooser.setFileFilter(new OnlyTxtFilter());

        authorDialog.setTitle("О аутору");
        authorDialog.setMinimumSize(new java.awt.Dimension(410, 150));
        authorDialog.setResizable(false);

        authorAvatarLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/desktopstrips/icons/avatar_100.png"))); // NOI18N

        authorNameLabel.setFont(new java.awt.Font("Courier New", 1, 18));
        authorNameLabel.setForeground(new java.awt.Color(0, 0, 0));
        authorNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        authorNameLabel.setText(" Мирко Француски");

        authorIndexLabel.setFont(new java.awt.Font("Courier New", 1, 18));
        authorIndexLabel.setForeground(new java.awt.Color(0, 0, 0));
        authorIndexLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        authorIndexLabel.setText(" 225/06");

        authorMailLabel.setFont(new java.awt.Font("Courier New", 1, 18));
        authorMailLabel.setForeground(new java.awt.Color(0, 0, 255));
        authorMailLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        authorMailLabel.setText(" fm060225d@student.etf.rs");

        javax.swing.GroupLayout authorDialogLayout = new javax.swing.GroupLayout(authorDialog.getContentPane());
        authorDialog.getContentPane().setLayout(authorDialogLayout);
        authorDialogLayout.setHorizontalGroup(
            authorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(authorDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(authorAvatarLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(authorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(authorIndexLabel)
                    .addComponent(authorMailLabel)
                    .addComponent(authorNameLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        authorDialogLayout.setVerticalGroup(
            authorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(authorDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(authorDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(authorDialogLayout.createSequentialGroup()
                        .addComponent(authorNameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(authorIndexLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(authorMailLabel))
                    .addComponent(authorAvatarLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        authorDialog.setLocationRelativeTo(rootPane);

        aboutDialog.setTitle("О програму");
        aboutDialog.setMinimumSize(new java.awt.Dimension(390, 180));
        aboutDialog.setResizable(false);

        aboutNameLabel.setFont(new java.awt.Font("Courier New", 1, 18));
        aboutNameLabel.setForeground(new java.awt.Color(0, 0, 255));
        aboutNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        aboutNameLabel.setText("Свет блокова");

        aboutBody.setText("<html>Имплементација аутомаског играча у игри <br>,,Свет блокова\" користећи СТРИПС алгоритам. <br>Опис проблема дат је у тексту задатка на адреси:");

        aboutOKButton.setText("Затвори");
        aboutOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutOKButtonActionPerformed(evt);
            }
        });

        aboutURL.setForeground(new java.awt.Color(0, 0, 255));
        aboutURL.setText("http://ri4es.etf.rs/domaci/ES_domaci_junski2011.pdf");
        aboutURL.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aboutURLMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout aboutDialogLayout = new javax.swing.GroupLayout(aboutDialog.getContentPane());
        aboutDialog.getContentPane().setLayout(aboutDialogLayout);
        aboutDialogLayout.setHorizontalGroup(
            aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutDialogLayout.createSequentialGroup()
                .addGroup(aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(aboutDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(aboutBody, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(aboutDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(aboutOKButton)
                            .addComponent(aboutURL)))
                    .addGroup(aboutDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(aboutNameLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        aboutDialogLayout.setVerticalGroup(
            aboutDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(aboutDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(aboutNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aboutBody, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(aboutURL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(aboutOKButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        aboutDialog.setLocationRelativeTo(rootPane);

        helpDialog.setTitle("Помоћ");
        helpDialog.setMinimumSize(new java.awt.Dimension(650, 300));
        helpDialog.setResizable(false);

        helpBodyDialog.setText("<html><p>Програм се покреће уношењем стања у облику текстуалне датотеке, где први ред садржи почетно стање, а други циљно.</p><br><p>Аутоматски се пријказује почетно стање. Затим се кликом миша на радну површину приказује циљно стање. Следећи клик приказује почетно стање поново.</p><br><p>Сваки следећи клик мишем ће приказати ново стање и извршену акцију, док се не дође до циљног стања.</p><br><p>Циљно стање можемо увек приказати кликом миша на листу предиката циљног стања која се налази у десном делу прозора. Враћање на тренутно стање се врши кликом миша било где унутар прозора. </p>");

        helpNameLabel.setFont(new java.awt.Font("Courier New", 1, 18));
        helpNameLabel.setForeground(new java.awt.Color(0, 0, 255));
        helpNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        helpNameLabel.setText("Како користити програм");

        helpOKButton.setText("Хвала");
        helpOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpOKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout helpDialogLayout = new javax.swing.GroupLayout(helpDialog.getContentPane());
        helpDialog.getContentPane().setLayout(helpDialogLayout);
        helpDialogLayout.setHorizontalGroup(
            helpDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(helpDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(helpDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(helpBodyDialog, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .addComponent(helpNameLabel)
                    .addComponent(helpOKButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        helpDialogLayout.setVerticalGroup(
            helpDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(helpDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(helpNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(helpBodyDialog, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(helpOKButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        helpDialog.setLocationRelativeTo(rootPane);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Свет блокова");
        setMinimumSize(new java.awt.Dimension(1000, 600));
        setName("frame"); // NOI18N
        setResizable(false);

        mainPanel.setVisible(false);
        mainPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainPanelMouseClicked(evt);
            }
        });

        actionPreformed.setFont(new java.awt.Font("Courier New", 1, 18));
        actionPreformed.setForeground(new java.awt.Color(0, 0, 255));
        actionPreformed.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        actionPreformed.setText("action");

        currentStateLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        currentStateLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        currentStateLabel.setText("Тренутно стање");

        finalStateLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        finalStateLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        finalStateLabel.setText("Циљно стање");

        currentStateTextArea.setColumns(1);
        currentStateTextArea.setEditable(false);
        currentStateTextArea.setRows(20);
        jScrollPaneLeft.setViewportView(currentStateTextArea);

        finalStateTextArea.setColumns(1);
        finalStateTextArea.setEditable(false);
        finalStateTextArea.setRows(20);
        finalStateTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                finalStateTextAreaMouseClicked(evt);
            }
        });
        jScrollPaneRight.setViewportView(finalStateTextArea);

        blockWorld.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        javax.swing.GroupLayout blockWorldLayout = new javax.swing.GroupLayout(blockWorld);
        blockWorld.setLayout(blockWorldLayout);
        blockWorldLayout.setHorizontalGroup(
            blockWorldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 588, Short.MAX_VALUE)
        );
        blockWorldLayout.setVerticalGroup(
            blockWorldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 302, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(currentStateLabel))
                .addGap(9, 9, 9)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(blockWorld, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPaneRight, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(actionPreformed)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
                        .addComponent(finalStateLabel)
                        .addGap(24, 24, 24))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(currentStateLabel)
                    .addComponent(finalStateLabel)
                    .addComponent(actionPreformed))
                .addGap(10, 10, 10)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blockWorld, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPaneLeft, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                    .addComponent(jScrollPaneRight, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE))
                .addContainerGap())
        );

        FileMenu.setText("Мени");

        Open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        Open.setText("Отвори");
        Open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenActionPerformed(evt);
            }
        });
        FileMenu.add(Open);

        Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        Exit.setText("Изађи");
        Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitActionPerformed(evt);
            }
        });
        FileMenu.add(Exit);

        jMenuBar.add(FileMenu);

        HelpMenu.setText("Помоћ");

        Help.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        Help.setText("Помоћ");
        Help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HelpActionPerformed(evt);
            }
        });
        HelpMenu.add(Help);
        HelpMenu.add(jSeparator);

        About.setText("О програму");
        About.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AboutActionPerformed(evt);
            }
        });
        HelpMenu.add(About);

        Author.setText("Аутор");
        Author.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AuthorActionPerformed(evt);
            }
        });
        HelpMenu.add(Author);

        jMenuBar.add(HelpMenu);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenActionPerformed
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            String[] states = StateParser.parseFile(file.getAbsolutePath());
            currentState = StateParser.parseStatesInLine(states[0]);
            finalState = StateParser.parseStatesInLine(states[1]);
            stripsAlgorithm = new Strips(currentState, finalState);

            actionPreformed.setText("Inital State");
            currentStateTextArea.setText("");
            stateToTextArea(currentStateTextArea, currentState);

            actionPreformed.setVisible(true);
            currentStateLabel.setVisible(true);
            finalStateLabel.setVisible(false);

            ((BlockWorld) blockWorld).setCurrentState(currentState);
            ((BlockWorld) blockWorld).clearGrid();

            if (mainPanel.isVisible()) {
                mainPanel.repaint();
            } else {
                mainPanel.setVisible(true);
            }

        } else {
            JOptionPane.showMessageDialog(rootPane, "Приступ документу отказан од стране корисника", "Oтказано", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_OpenActionPerformed

    private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_ExitActionPerformed

    private void mainPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseClicked

        if (!finalStateLabel.isVisible()) {
            finalStateLabel.setVisible(true);
            actionPreformed.setText("Final State");
            finalStateTextArea.setText("");
            stateToTextArea(finalStateTextArea, finalState);
            ((BlockWorld) blockWorld).setCurrentState(finalState);
            ((BlockWorld) blockWorld).clearGrid();
            mainPanel.repaint();
            return;
        }

        if (actionPreformed.getText().equals("Final State")) {
            String actionName;

            try {
                actionName = stripsAlgorithm.getLastAction().toString();
            } catch (Exception e) {
                actionName = "Inital State";
            }

            actionPreformed.setText(actionName);
            ((BlockWorld) blockWorld).setCurrentState(currentState);
            ((BlockWorld) blockWorld).clearGrid();
            mainPanel.repaint();
            return;
        }

        if ((currentState = stripsAlgorithm.getNextState()) != null) {

            actionPreformed.setText(stripsAlgorithm.getLastAction().toString());
            currentStateTextArea.setText("");
            stateToTextArea(currentStateTextArea, currentState);

            ((BlockWorld) blockWorld).setCurrentState(currentState);
            mainPanel.repaint();
            return;

        } else {
            actionPreformed.setText("Final State Achieved!");
            actionPreformed.repaint();

            Iterator<strips.Action> it = stripsAlgorithm.getActions().iterator();
            String act = "";
            while (it.hasNext()) {
                act += it.next() + "\n";
            }

            JOptionPane.showMessageDialog(rootPane, act, "Акције извршене", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_mainPanelMouseClicked

    private void finalStateTextAreaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_finalStateTextAreaMouseClicked
        if (!actionPreformed.getText().startsWith("Final State")) {
            actionPreformed.setText("Final State");
            finalStateTextArea.setText("");
            stateToTextArea(finalStateTextArea, finalState);
            ((BlockWorld) blockWorld).setCurrentState(finalState);
            ((BlockWorld) blockWorld).clearGrid();
            mainPanel.repaint();
            return;
        }
    }//GEN-LAST:event_finalStateTextAreaMouseClicked

    private void AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutActionPerformed
        aboutDialog.setVisible(true);
    }//GEN-LAST:event_AboutActionPerformed

    private void AuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AuthorActionPerformed
        authorDialog.setVisible(true);
    }//GEN-LAST:event_AuthorActionPerformed

    private void aboutOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutOKButtonActionPerformed
        aboutDialog.setVisible(false);
    }//GEN-LAST:event_aboutOKButtonActionPerformed

    private void aboutURLMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aboutURLMouseClicked
        try {
            if (java.awt.Desktop.isDesktopSupported()) {
                java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
                if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
                    java.net.URI uri = new java.net.URI("http://ri4es.etf.rs/domaci/ES_domaci_junski2011.pdf");
                    desktop.browse(uri);
                }
            }
        } catch (URISyntaxException uRISyntaxException) {
        } catch (IOException iOException) {
        }
    }//GEN-LAST:event_aboutURLMouseClicked

    private void helpOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpOKButtonActionPerformed
        helpDialog.setVisible(false);
    }//GEN-LAST:event_helpOKButtonActionPerformed

    private void HelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HelpActionPerformed
        helpDialog.setVisible(true);
    }//GEN-LAST:event_HelpActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DesktopStrips().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem About;
    private javax.swing.JMenuItem Author;
    private javax.swing.JMenuItem Exit;
    private javax.swing.JMenu FileMenu;
    private javax.swing.JMenuItem Help;
    private javax.swing.JMenu HelpMenu;
    private javax.swing.JMenuItem Open;
    private javax.swing.JLabel aboutBody;
    private javax.swing.JDialog aboutDialog;
    private javax.swing.JLabel aboutNameLabel;
    private javax.swing.JButton aboutOKButton;
    private javax.swing.JLabel aboutURL;
    private javax.swing.JLabel actionPreformed;
    private javax.swing.JLabel authorAvatarLabel;
    private javax.swing.JDialog authorDialog;
    private javax.swing.JLabel authorIndexLabel;
    private javax.swing.JLabel authorMailLabel;
    private javax.swing.JLabel authorNameLabel;
    private javax.swing.JPanel blockWorld;
    private javax.swing.JLabel currentStateLabel;
    private javax.swing.JTextArea currentStateTextArea;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JLabel finalStateLabel;
    private javax.swing.JTextArea finalStateTextArea;
    private javax.swing.JLabel helpBodyDialog;
    private javax.swing.JDialog helpDialog;
    private javax.swing.JLabel helpNameLabel;
    private javax.swing.JButton helpOKButton;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JScrollPane jScrollPaneLeft;
    private javax.swing.JScrollPane jScrollPaneRight;
    private javax.swing.JPopupMenu.Separator jSeparator;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
    private ArrayDeque<Predicate> currentState;
    private ArrayDeque<Predicate> finalState;
    private Strips stripsAlgorithm;
}
